package com.huiyeji.dynamic.core.parser;

import com.huiyeji.dynamic.core.DynamicContext;
import com.huiyeji.dynamic.core.DynamicRegex;
import com.huiyeji.dynamic.core.DynamicStatement;
import com.huiyeji.dynamic.core.RegexContainer;
import com.huiyeji.dynamic.core.handler.DynamicBuilderPrefixHandler;
import com.huiyeji.dynamic.core.provider.IntegerParamProvider;
import com.huiyeji.dynamic.core.provider.ParamProvider;
import com.huiyeji.dynamic.core.provider.StringParamProvider;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DirectAssignmentParser implements DynamicParser{
    @Override
    public DynamicStatement parse(DynamicContext context, String statement) {
        String[] statementArray = statement.split("=", 2);
        String resultName = statementArray[0].trim();
        boolean isString = statement.contains("'");
        String dynamicObjectName = isString ?"string":"integer";
        String paramName = statementArray[1].trim();
        List<ParamProvider> paramProviders = new ArrayList<>();
        paramProviders.add(isString?new StringParamProvider(paramName):new IntegerParamProvider(paramName));
        return new DynamicStatement(
                statement,
                true,
                resultName,
                null,
                dynamicObjectName,
                "of",
                null,
                paramProviders
        );
    }

    @Override
    public RegexContainer ruleInitialize() {
        return new RegexContainer(Collections.singletonList(
                DynamicRegex.build(true,
                        DynamicRegex.PARAM_CONTEXT_OBJECT_NAME.origin(),
                        "=",
                        "(" + DynamicRegex.PARAM_STRING.origin() + "|" + DynamicRegex.PARAM_INTEGER.origin() + ")")
        ));
    }

    @Override
    public int getOrder() {
        return 10;
    }
}
